<template>
  <view v-if="show" class="team-invitation-modal">
    <view class="modal-overlay" @click="closeModal"></view>
    <view class="modal-content">
      <view class="modal-header">
        <text class="modal-title">创建团队邀请</text>
        <button class="close-btn" @click="closeModal">×</button>
      </view>
      
      <view class="modal-body">
        <!-- 团队信息显示 -->
        <view v-if="teamInfo" class="team-info-section">
          <text class="section-title">团队信息</text>
          <view class="team-info-item">
            <text class="info-label">团队名称：</text>
            <view class="team-name-edit">
              <input 
                v-model="editingTeamName" 
                class="team-name-input" 
                placeholder="请输入团队名称"
                maxlength="20"
              />
              <button class="save-name-btn" @click="saveTeamName" :disabled="!editingTeamName.trim()">保存</button>
            </view>
          </view>
          <view class="team-info-item">
            <text class="info-label">团队状态：</text>
            <text class="info-value">{{ getTeamStatusText(teamInfo.status) }}</text>
          </view>
        </view>
        
        <!-- 团队成员显示 -->
        <view v-if="teamInfo && teamInfo.members && teamInfo.members.length > 0" class="team-members-section">
          <text class="section-title">团队成员 ({{ teamInfo.members.length }})</text>
          <view class="members-list">
            <view 
              v-for="member in teamInfo.members" 
              :key="member.userId"
              class="member-item"
            >
              <image :src="getUserAvatar(member.userId)" class="member-avatar" @error="handleAvatarError" />
              <view class="member-info">
                <text class="member-name">{{ getUserNickname(member.userId) }}</text>
                <text class="member-role">{{ member.role === 'leader' ? '队长' : '队员' }}</text>
                <text class="member-status" :class="'status-' + member.status">{{ member.statusText }}</text>
              </view>
            </view>
          </view>
        </view>
        
        <!-- 待处理邀请显示 -->
        <view v-if="teamInfo && teamInfo.pendingInvitations && teamInfo.pendingInvitations.length > 0" class="pending-invitations-section">
          <text class="section-title">待处理邀请 ({{ teamInfo.pendingInvitations.length }})</text>
          <view class="invitations-list">
            <view 
              v-for="invitation in teamInfo.pendingInvitations" 
              :key="invitation.userId"
              class="invitation-item"
            >
              <image :src="getUserAvatar(invitation.userId)" class="invitation-avatar" @error="handleAvatarError" />
              <view class="invitation-info">
                <text class="invitation-name">{{ getUserNickname(invitation.userId) }}</text>
                <text class="invitation-time">{{ formatTime(invitation.sendTime) }}</text>
              </view>
            </view>
          </view>
        </view>
        
        <!-- 团队名称输入（仅在没有团队信息时显示） -->
        <view v-if="!teamInfo" class="form-group">
          <text class="form-label">团队名称</text>
          <input 
            v-model="teamName" 
            class="form-input" 
            placeholder="请输入团队名称"
            maxlength="20"
          />
        </view>
        
         <!-- 好友选择 -->
         <view class="form-group">
           <text class="form-label">邀请成员</text>
           <text class="form-hint">可以邀请好友或搜索其他用户加入团队</text>
           
           <!-- 已选择的好友显示 -->
           <view v-if="selectedFriends.length > 0" class="selected-friends-display">
             <text class="selected-title">已选择的好友 ({{ selectedFriends.length }})</text>
             <view class="selected-friends-list">
               <view 
                 v-for="friendId in selectedFriends" 
                 :key="friendId"
                 class="selected-friend-item"
               >
                 <image :src="getSelectedFriendAvatar(friendId)" class="selected-friend-avatar" @error="handleAvatarError" />
                 <text class="selected-friend-name">{{ getSelectedFriendName(friendId) }}</text>
                 <button class="remove-selected-btn" @click="removeFriend(friendId)">×</button>
               </view>
             </view>
           </view>
           
           <view class="friend-search-section">
             <!-- 搜索框 -->
             <view class="search-input-group">
               <input 
                 v-model="searchKeyword" 
                 class="search-input" 
                 placeholder="输入好友昵称或UID搜索"
                 @input="searchFriends"
               />
               <button class="search-btn" @click="searchFriends">搜索</button>
             </view>
             
             <!-- 搜索结果 -->
             <view v-if="searchLoading" class="search-loading">搜索中...</view>
             <view v-else-if="searchError" class="search-error">{{ searchError }}</view>
             <view v-else-if="searchResults.length === 0 && searchKeyword" class="search-empty">
               没有找到相关好友
             </view>
             <view v-else-if="searchResults.length > 0" class="search-results">
               <view 
                 v-for="friend in searchResults" 
                 :key="friend.friendId || friend.uid || friend.id"
                 class="search-result-item"
                 :class="{ 
                   'already-selected': selectedFriends.includes(friend.friendId || friend.uid || friend.id),
                   'is-friend': friend.friendStatus === 'accepted',
                   'not-friend': friend.friendStatus !== 'accepted'
                 }"
               >
                 <image :src="getAvatarUrl(friend.avatarUrl || friend.friendAvatarUrl)" class="search-friend-avatar" @error="handleAvatarError" />
                 <view class="search-friend-info">
                   <view class="friend-name-row">
                     <text class="search-friend-name">{{ friend.friendNickname || friend.nickname || '未知用户' }}</text>
                     <view class="friend-status-badge" :class="friend.friendStatus">
                       <text v-if="friend.friendStatus === 'accepted'" class="status-text">好友</text>
                       <text v-else-if="friend.friendStatus === 'pending'" class="status-text">待确认</text>
                       <text v-else class="status-text">非好友</text>
                     </view>
                   </view>
                   <text class="search-friend-uid">UID: {{ friend.friendId || friend.uid || friend.id }}</text>
                 </view>
                 <view class="search-result-actions">
                   <button 
                     v-if="!isUserInvited(friend.friendId || friend.uid || friend.id)"
                     class="invite-btn"
                     @click="inviteSingleUser(friend.friendId || friend.uid || friend.id)"
                   >
                     邀请
                   </button>
                   <view v-else class="invited-status">
                     <text class="invited-text">已邀请</text>
                   </view>
                   <button 
                     v-if="!selectedFriends.includes(friend.friendId || friend.uid || friend.id)"
                     class="add-btn"
                     @click="addFriendFromSearch(friend)"
                   >
                     添加
                   </button>
                   <view v-else class="added-status">
                     <text class="added-text">已添加</text>
                   </view>
                 </view>
               </view>
             </view>
             <view v-else class="search-placeholder">
               请输入好友昵称或UID开始搜索
             </view>
           </view>
         </view>
        
        
      </view>
      
      <view class="modal-footer">
        <button class="cancel-btn" @click="closeModal">取消</button>
        <button 
          class="confirm-btn" 
          :disabled="!canConfirm"
          @click="confirmInvitation"
        >
          发送邀请
        </button>
      </view>
    </view>
    
  </view>
</template>

<script setup>
import { ref, computed, onMounted, watch } from 'vue'
import { NETWORK_CONFIG } from '@/config/network.js'
import { getAvatarUrl } from '@/utils/imageUtils.js'

// Props
const props = defineProps({
  show: {
    type: Boolean,
    default: false
  },
  taskId: {
    type: [String, Number],
    required: true
  },
  teamInfo: {
    type: Object,
    default: null
  }
})

// Emits
const emit = defineEmits(['close', 'confirm'])

// 数据
const teamName = ref('')
const friends = ref([])
const selectedFriends = ref([])
const editingTeamName = ref('')

// 解析JWT token的工具函数（兼容微信小程序）
const parseJwtToken = (token) => {
  try {
    const tokenParts = token.split('.');
    if (tokenParts.length !== 3) {
      throw new Error('Invalid token format');
    }
    
    const payloadBase64 = tokenParts[1];
    // 添加填充字符以确保 base64 长度正确
    const paddedBase64 = payloadBase64 + '='.repeat((4 - payloadBase64.length % 4) % 4);
    
    // 在微信小程序环境中使用 uni.base64ToArrayBuffer
    const arrayBuffer = uni.base64ToArrayBuffer(paddedBase64);
    const payloadString = String.fromCharCode.apply(null, new Uint8Array(arrayBuffer));
    return JSON.parse(payloadString);
  } catch (error) {
    console.error('解析JWT token失败:', error);
    return null;
  }
};
const userInfoCache = ref({}) // 用户信息缓存

// 搜索相关数据
const searchKeyword = ref('')
const searchResults = ref([])
const searchLoading = ref(false)
const searchError = ref(null)

// 计算属性
const availableFriends = computed(() => {
  return friends.value
})

const canConfirm = computed(() => {
  return teamName.value.trim() && selectedFriends.value.length > 0
})

// 方法
const loadFriends = async () => {
  try {
    const token = uni.getStorageSync('accessToken')
    const res = await uni.request({
      url: `${NETWORK_CONFIG.API_BASE_URL}/friends`,
      method: 'GET',
      header: { 'Access-Token': `Bearer ${token}` }
    })
    
    if (res.data && res.data.code === 200) {
      friends.value = res.data.data || []
    }
  } catch (error) {
    console.error('加载好友列表失败:', error)
    uni.showToast({
      title: '加载好友失败',
      icon: 'none'
    })
  }
}

const toggleFriend = (friendId) => {
  const index = selectedFriends.value.indexOf(friendId)
  if (index > -1) {
    selectedFriends.value.splice(index, 1)
  } else {
    selectedFriends.value.push(friendId)
  }
}

const removeFriend = (friendId) => {
  const index = selectedFriends.value.indexOf(friendId)
  if (index > -1) {
    selectedFriends.value.splice(index, 1)
  }
}

const getFriendName = (friendId) => {
  const friend = friends.value.find(f => (f.friendId || f.id) === friendId)
  return friend ? (friend.nickname || friend.friendNickname) : '未知用户'
}

const getSelectedFriendName = (friendId) => {
  // 先从搜索结果中查找
  const searchFriend = searchResults.value.find(f => (f.friendId || f.uid || f.id) === friendId)
  if (searchFriend) {
    return searchFriend.friendNickname || searchFriend.nickname || '未知用户'
  }
  
  // 再从好友列表中查找
  const friend = friends.value.find(f => (f.friendId || f.id) === friendId)
  return friend ? (friend.nickname || friend.friendNickname) : '未知用户'
}

const getSelectedFriendAvatar = (friendId) => {
  // 先从搜索结果中查找
  const searchFriend = searchResults.value.find(f => (f.friendId || f.uid || f.id) === friendId)
  if (searchFriend) {
    return getAvatarUrl(searchFriend.avatarUrl || searchFriend.friendAvatarUrl)
  }
  
  // 再从好友列表中查找
  const friend = friends.value.find(f => (f.friendId || f.id) === friendId)
  return friend ? getAvatarUrl(friend.avatarUrl || friend.friendAvatarUrl) : '/static/images/default-avatar.png'
}

const confirmInvitation = async () => {
  try {
    const token = uni.getStorageSync('accessToken')
    const currentUser = uni.getStorageSync('userInfo')
    
    // 获取用户ID
    let leaderId = null
    
    // 首先尝试从存储的用户信息获取
    if (currentUser && currentUser.uid) {
      leaderId = currentUser.uid
      console.log('从用户信息获取leaderId:', leaderId)
    } else {
      // 如果用户信息无效，从token中解析
      try {
        const tokenPayload = parseJwtToken(token)
        console.log('从token解析用户信息:', tokenPayload)
        
        if (tokenPayload && (tokenPayload.uid || tokenPayload.sub)) {
          leaderId = tokenPayload.uid || tokenPayload.sub
          console.log('从token获取leaderId:', leaderId)
        }
      } catch (tokenError) {
        console.error('解析token失败:', tokenError)
      }
    }
    
    // 检查是否成功获取用户ID
    if (!leaderId) {
      uni.showToast({
        title: '用户信息获取失败，请重新登录',
        icon: 'none'
      })
      return
    }
    
    const respCreate = await uni.request({
      url: `${NETWORK_CONFIG.API_BASE_URL}/team/create-invitation`,
      method: 'POST',
      data: { taskId: Number(props.taskId), leaderId: Number(leaderId), teamName: teamName.value.trim(), inviteeIds: selectedFriends.value.map(id => Number(id)) },
      header: { 'Access-Token': `Bearer ${token}`, 'Content-Type': 'application/json' }
    })
    
    if (respCreate.data && respCreate.data.success) {
      uni.showToast({
        title: '邀请发送成功',
        icon: 'success'
      })
      emit('confirm', respCreate.data.teamId)
      closeModal()
    } else {
      uni.showToast({
        title: respCreate.data?.message || '发送邀请失败',
        icon: 'none'
      })
    }
  } catch (error) {
    console.error('发送邀请失败:', error)
    uni.showToast({
      title: '发送邀请失败',
      icon: 'none'
    })
  }
}

// 单独邀请某个用户
const inviteSingleUser = async (friendId) => {
  try {
    const token = uni.getStorageSync('accessToken')
    const currentUser = uni.getStorageSync('userInfo')
    
    // 获取用户ID
    let leaderId = null
    
    // 首先尝试从存储的用户信息获取
    if (currentUser && currentUser.uid) {
      leaderId = currentUser.uid
      console.log('从用户信息获取leaderId:', leaderId)
    } else {
      // 如果用户信息无效，从token中解析
      try {
        const tokenPayload = parseJwtToken(token)
        console.log('从token解析用户信息:', tokenPayload)
        
        if (tokenPayload && (tokenPayload.uid || tokenPayload.sub)) {
          leaderId = tokenPayload.uid || tokenPayload.sub
          console.log('从token获取leaderId:', leaderId)
        }
      } catch (tokenError) {
        console.error('解析token失败:', tokenError)
      }
    }
    
    // 检查是否成功获取用户ID
    if (!leaderId) {
      uni.showToast({
        title: '用户信息获取失败，请重新登录',
        icon: 'none'
      })
      return
    }
    
    // 检查是否尝试邀请自己
    if (String(leaderId) === String(friendId)) {
      uni.showToast({
        title: '不能邀请自己加入团队',
        icon: 'none'
      })
      return
    }
    
    console.log('邀请参数调试:', {
      taskId: props.taskId,
      leaderId: leaderId,
      teamName: teamName.value.trim() || '临时团队',
      inviteeIds: [friendId],
      token: token ? '存在' : '不存在'
    })
    
    const respInvite = await uni.request({
      url: `${NETWORK_CONFIG.API_BASE_URL}/team/send-invitation`,
      method: 'POST',
      data: { taskId: Number(props.taskId), leaderId: Number(leaderId), teamName: teamName.value.trim() || '临时团队', inviteeIds: [Number(friendId)] },
      header: { 'Access-Token': `Bearer ${token}`, 'Content-Type': 'application/json' }
    })
    
    console.log('邀请响应:', respInvite)
    
    if (respInvite.data && respInvite.data.success) {
      uni.showToast({
        title: '邀请发送成功',
        icon: 'success'
      })
      // 标记该用户已发送邀请
      markUserAsInvited(friendId)
    } else {
      console.error('邀请失败响应:', respInvite.data)
      uni.showToast({
        title: respInvite.data?.message || '发送邀请失败',
        icon: 'none'
      })
    }
  } catch (error) {
    console.error('发送邀请失败:', error)
    uni.showToast({
      title: '发送邀请失败',
      icon: 'none'
    })
  }
}

// 标记用户已发送邀请
const invitedUsers = ref(new Set())

const markUserAsInvited = (friendId) => {
  invitedUsers.value.add(friendId)
}

const isUserInvited = (friendId) => {
  return invitedUsers.value.has(friendId)
}

const closeModal = () => {
  emit('close')
}


// 搜索好友相关方法

const searchFriends = async () => {
  if (!searchKeyword.value.trim()) {
    searchResults.value = []
    return
  }
  
  searchLoading.value = true
  searchError.value = null
  
  try {
    const token = uni.getStorageSync('accessToken')
    const resSearch = await uni.request({
      url: `${NETWORK_CONFIG.API_BASE_URL}/friends/search?keyword=${encodeURIComponent(searchKeyword.value)}`,
      method: 'GET',
      header: { 'Access-Token': `Bearer ${token}` }
    })
    
    if (resSearch.data && resSearch.data.code === 200) {
      // 显示所有搜索结果，但优先显示好友
      const allResults = resSearch.data.data || []
      
      // 按好友状态排序：好友在前，非好友在后
      searchResults.value = allResults.sort((a, b) => {
        if (a.friendStatus === 'accepted' && b.friendStatus !== 'accepted') return -1
        if (a.friendStatus !== 'accepted' && b.friendStatus === 'accepted') return 1
        return 0
      })
      
      console.log('搜索结果:', searchResults.value)
      console.log('搜索结果数量:', searchResults.value.length)
      console.log('好友数量:', allResults.filter(u => u.friendStatus === 'accepted').length)
      console.log('非好友数量:', allResults.filter(u => u.friendStatus !== 'accepted').length)
    } else {
      searchError.value = resSearch.data?.message || '搜索失败'
    }
  } catch (error) {
    console.error('搜索好友失败:', error)
    searchError.value = '搜索失败，请重试'
  } finally {
    searchLoading.value = false
  }
}

const addFriendFromSearch = (friend) => {
  const friendId = friend.friendId || friend.uid || friend.id
  if (!selectedFriends.value.includes(friendId)) {
    selectedFriends.value.push(friendId)
    uni.showToast({
      title: '已添加好友',
      icon: 'success'
    })
  } else {
    uni.showToast({
      title: '该好友已在列表中',
      icon: 'none'
    })
  }
}

const clearSearchAndAddMore = () => {
  searchKeyword.value = ''
  searchResults.value = []
  searchError.value = null
  // 在微信小程序中不需要聚焦，直接清空即可
}

// 团队相关方法
const saveTeamName = async () => {
  if (!editingTeamName.value.trim()) {
    uni.showToast({ title: '请输入团队名称', icon: 'none' })
    return
  }
  
  if (!props.teamInfo || !props.teamInfo.teamId) {
    uni.showToast({ title: '团队信息不存在', icon: 'none' })
    return
  }
  
  try {
    const token = uni.getStorageSync('accessToken')
    if (!token) {
      uni.showToast({ title: '请先登录', icon: 'none' })
      return
    }
    
    console.log('更新团队名称参数:', {
      teamId: props.teamInfo.teamId,
      teamName: editingTeamName.value.trim()
    })
    
    const resUpdate = await uni.request({
      url: `${NETWORK_CONFIG.API_BASE_URL}/team/update-name`,
      method: 'POST',
      data: { teamId: Number(props.teamInfo.teamId), teamName: editingTeamName.value.trim() },
      header: { 'Access-Token': `Bearer ${token}`, 'Content-Type': 'application/json' }
    })
    
    console.log('更新团队名称响应:', resUpdate)
    
    if (resUpdate.data && resUpdate.data.success) {
      uni.showToast({ title: '团队名称更新成功', icon: 'success' })
      // 通知父组件更新团队信息
      emit('updateTeamName', editingTeamName.value.trim())
    } else {
      uni.showToast({ title: resUpdate.data?.message || '更新失败', icon: 'none' })
    }
  } catch (error) {
    console.error('更新团队名称失败:', error)
    uni.showToast({ title: '更新团队名称失败', icon: 'none' })
  }
}

const getTeamStatusText = (status) => {
  switch (status) {
    case 0: return '组建中'
    case 1: return '已组建'
    case 2: return '已解散'
    default: return '未知状态'
  }
}

const formatTime = (timeStr) => {
  if (!timeStr) return ''
  const date = new Date(timeStr)
  const now = new Date()
  const diff = now - date
  
  if (diff < 60000) return '刚刚'
  if (diff < 3600000) return Math.floor(diff / 60000) + '分钟前'
  if (diff < 86400000) return Math.floor(diff / 3600000) + '小时前'
  return Math.floor(diff / 86400000) + '天前'
}

const handleAvatarError = (e) => {
  console.log('头像加载失败:', e)
}

// 获取用户信息
const getUserInfo = async (userId) => {
  if (userInfoCache.value[userId]) {
    return userInfoCache.value[userId]
  }
  
  try {
    const token = uni.getStorageSync('accessToken')
    if (!token) return null
    
    console.log('获取用户信息，userId:', userId)
    
    const resUser = await uni.request({
      url: `${NETWORK_CONFIG.API_BASE_URL}/user/info`,
      method: 'POST',
      data: { uid: userId },
      header: { 'Access-Token': `Bearer ${token}`, 'Content-Type': 'application/json' }
    })
    
    console.log('用户信息响应:', resUser)
    
    if (resUser.statusCode === 200 && resUser.data) {
      // 后端直接返回用户信息对象，不需要检查 code 字段
      const userInfo = resUser.data
      userInfoCache.value[userId] = userInfo
      console.log('用户信息缓存成功:', userInfo)
      return userInfo
    } else {
      console.error('获取用户信息失败，响应:', resUser.data)
    }
  } catch (error) {
    console.error('获取用户信息失败:', error)
  }
  
  return null
}

// 获取用户昵称
const getUserNickname = (userId) => {
  const userInfo = userInfoCache.value[userId]
  return userInfo ? userInfo.nickname : '加载中...'
}

// 获取用户头像
const getUserAvatar = (userId) => {
  const userInfo = userInfoCache.value[userId]
  console.log('获取用户头像，userId:', userId, 'userInfo:', userInfo)
  
  if (userInfo && userInfo.avatarUrl) {
    if (userInfo.avatarUrl.startsWith('/')) {
      const fullUrl = `${NETWORK_CONFIG.API_BASE_URL}${userInfo.avatarUrl}`
      console.log('头像URL（相对路径）:', fullUrl)
      return fullUrl
    }
    return userInfo.avatarUrl
  }
  
  console.log('使用默认头像')
  return '/static/images/default-avatar.png'
}

// 监听show属性变化
watch(() => props.show, (newVal) => {
  if (newVal) {
    loadFriends()
    // 初始化编辑团队名称
    if (props.teamInfo && props.teamInfo.teamName) {
      editingTeamName.value = props.teamInfo.teamName
    }
    // 加载用户信息
    loadUserInfos()
  }
}, { immediate: true })

// 监听团队信息变化
watch(() => props.teamInfo, (newTeamInfo) => {
  if (newTeamInfo) {
    console.log('团队信息变化，重新加载用户信息:', newTeamInfo)
    // 清除 invitedUsers 缓存，确保成员列表更新后能正确显示
    invitedUsers.value.clear();
    
    // 如果团队有成员，将他们标记为已邀请（只有当前团队成员才显示"已邀请"）
    if (newTeamInfo.members && newTeamInfo.members.length > 0) {
      newTeamInfo.members.forEach(member => {
        invitedUsers.value.add(member.userId);
      });
      console.log('添加团队成员到已邀请列表:', newTeamInfo.members.map(m => m.userId));
    }
    
    // 将待处理邀请的用户也标记为已邀请
    if (newTeamInfo.pendingInvitations && newTeamInfo.pendingInvitations.length > 0) {
      newTeamInfo.pendingInvitations.forEach(invitation => {
        if (invitation.userId) {
          invitedUsers.value.add(invitation.userId);
        }
      });
      console.log('添加待处理邀请到已邀请列表:', newTeamInfo.pendingInvitations.map(i => i.userId));
    }
    
    console.log('团队信息变化后，更新已邀请用户列表（团队成员 + 待处理邀请）:', Array.from(invitedUsers.value));
    
    // 初始化编辑团队名称
    if (newTeamInfo.teamName) {
      editingTeamName.value = newTeamInfo.teamName
    }
    // 重新加载用户信息
    loadUserInfos()
  } else {
    // 如果团队信息为空，清除所有缓存
    invitedUsers.value.clear();
    console.log('团队信息为空，清除已邀请用户列表');
  }
}, { deep: true })

// 加载所有用户信息
const loadUserInfos = async () => {
  if (!props.teamInfo) return
  
  console.log('开始加载用户信息，团队信息:', props.teamInfo)
  
  // 加载团队成员信息
  if (props.teamInfo.members) {
    console.log('加载团队成员信息，成员数量:', props.teamInfo.members.length)
    for (const member of props.teamInfo.members) {
      console.log('加载成员信息，userId:', member.userId)
      await getUserInfo(member.userId)
    }
  }
  
  // 加载待处理邀请用户信息
  if (props.teamInfo.pendingInvitations) {
    console.log('加载待处理邀请信息，邀请数量:', props.teamInfo.pendingInvitations.length)
    for (const invitation of props.teamInfo.pendingInvitations) {
      console.log('加载邀请用户信息，userId:', invitation.userId)
      await getUserInfo(invitation.userId)
    }
  }
  
  console.log('用户信息加载完成，缓存:', userInfoCache.value)
}

// 生命周期
onMounted(() => {
  if (props.show) {
    loadFriends()
    // 初始化编辑团队名称
    if (props.teamInfo && props.teamInfo.teamName) {
      editingTeamName.value = props.teamInfo.teamName
    }
  }
})
</script>

<style scoped>
.team-invitation-modal {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  z-index: 1000;
  display: flex;
  align-items: center;
  justify-content: center;
}

.modal-overlay {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.5);
}

.modal-content {
  position: relative;
  width: 90%;
  max-width: 500px;
  max-height: 80vh;
  background: white;
  border-radius: 12px;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20px;
  border-bottom: 1px solid #eee;
}

.modal-title {
  font-size: 18px;
  font-weight: 600;
  color: #333;
}

.close-btn {
  width: 30px;
  height: 30px;
  border: none;
  background: #f5f5f5;
  border-radius: 50%;
  font-size: 18px;
  color: #666;
  cursor: pointer;
}

.modal-body {
  flex: 1;
  padding: 20px;
  overflow-y: auto;
}

.form-group {
  margin-bottom: 20px;
}

.form-label {
  display: block;
  font-size: 14px;
  font-weight: 600;
  color: #333;
  margin-bottom: 4px;
}

.form-hint {
  display: block;
  font-size: 12px;
  color: #666;
  margin-bottom: 8px;
  font-style: italic;
}

.form-input {
  width: 100%;
  padding: 12px;
  border: 1px solid #ddd;
  border-radius: 8px;
  font-size: 14px;
}

.friend-selector {
  max-height: 200px;
  overflow-y: auto;
  border: 1px solid #eee;
  border-radius: 8px;
}

.friend-item {
  display: flex;
  align-items: center;
  padding: 12px;
  border-bottom: 1px solid #f5f5f5;
  cursor: pointer;
  transition: background-color 0.3s;
}

.friend-item:hover {
  background: #f8f9fa;
}

.friend-item.selected {
  background: #e3f2fd;
}

.friend-avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  margin-right: 12px;
}

.friend-name {
  flex: 1;
  font-size: 14px;
  color: #333;
}

.selected-icon {
  width: 20px;
  height: 20px;
  background: #4CAF50;
  color: white;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 12px;
}

.selected-friends {
  margin-top: 16px;
}

.selected-title {
  font-size: 14px;
  font-weight: 600;
  color: #333;
  margin-bottom: 8px;
}

.selected-list {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.selected-item {
  display: flex;
  align-items: center;
  background: #e3f2fd;
  padding: 6px 12px;
  border-radius: 16px;
  font-size: 12px;
  color: #1976d2;
}

.remove-btn {
  width: 16px;
  height: 16px;
  border: none;
  background: #ff5722;
  color: white;
  border-radius: 50%;
  font-size: 10px;
  margin-left: 6px;
  cursor: pointer;
}

.modal-footer {
  display: flex;
  gap: 12px;
  padding: 20px;
  border-top: 1px solid #eee;
}

.cancel-btn,
.confirm-btn {
  flex: 1;
  padding: 12px;
  border: none;
  border-radius: 8px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
}

.cancel-btn {
  background: #f5f5f5;
  color: #666;
}

.confirm-btn {
  background: #4CAF50;
  color: white;
}

.confirm-btn:disabled {
  background: #ccc;
  cursor: not-allowed;
}

/* 好友搜索区域样式 */
.friend-search-section {
  margin-top: 8px;
}

.search-input-group {
  display: flex;
  gap: 8px;
  margin-bottom: 16px;
}

.search-input {
  flex: 1;
  border: 1px solid #ddd;
  border-radius: 8px;
  padding: 8px 12px;
  font-size: 14px;
}

.search-btn {
  background: #4A90E2;
  color: white;
  border: none;
  border-radius: 8px;
  padding: 8px 16px;
  font-size: 14px;
  cursor: pointer;
}

.search-loading, .search-error, .search-empty, .search-placeholder {
  text-align: center;
  color: #666;
  padding: 20px;
}

.search-error {
  color: #e74c3c;
}

.search-results {
  max-height: 300px;
  overflow-y: auto;
}

.search-result-item {
  display: flex;
  align-items: center;
  padding: 12px;
  border-bottom: 1px solid #f5f5f5;
  cursor: pointer;
  transition: background-color 0.2s;
}

.search-result-item:hover {
  background: #f8f9fa;
}

.search-result-item.already-selected {
  background: #e8f5e8;
  color: #2e7d32;
}

.search-friend-avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  margin-right: 12px;
  object-fit: cover;
}

.search-friend-info {
  flex: 1;
  display: flex;
  flex-direction: column;
}

.search-friend-name {
  font-size: 15px;
  font-weight: 500;
  color: #333;
  margin-bottom: 2px;
}

.search-friend-uid {
  font-size: 12px;
  color: #666;
}

.search-selected-icon {
  color: #2e7d32;
  font-weight: bold;
  font-size: 18px;
}

/* 好友状态样式 */
.friend-name-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 2px;
}

.friend-status-badge {
  padding: 2px 8px;
  border-radius: 12px;
  font-size: 10px;
  font-weight: 500;
}

.friend-status-badge.accepted {
  background: #e8f5e8;
  color: #2e7d32;
}

.friend-status-badge.pending {
  background: #fff3e0;
  color: #f57c00;
}

.friend-status-badge.not_friend {
  background: #f3e5f5;
  color: #7b1fa2;
}

.status-text {
  font-size: 10px;
  font-weight: 500;
}

/* 搜索结果项样式增强 */
.search-result-item.is-friend {
  border-left: 3px solid #4CAF50;
}

.search-result-item.not-friend {
  border-left: 3px solid #9C27B0;
  opacity: 0.9;
}

/* 搜索结果操作按钮样式 */
.search-result-actions {
  display: flex;
  flex-direction: column;
  gap: 4px;
  align-items: flex-end;
}

.invite-btn, .add-btn {
  padding: 4px 12px;
  border: none;
  border-radius: 4px;
  font-size: 12px;
  cursor: pointer;
  transition: all 0.3s ease;
}

.invite-btn {
  background: #4CAF50;
  color: white;
}

.invite-btn:hover {
  background: #45a049;
}

.add-btn {
  background: #2196F3;
  color: white;
}

.add-btn:hover {
  background: #1976D2;
}

.invited-status, .added-status {
  padding: 4px 8px;
  border-radius: 4px;
  font-size: 10px;
  text-align: center;
}

.invited-status {
  background: #e8f5e8;
  color: #2e7d32;
}

.added-status {
  background: #e3f2fd;
  color: #1976d2;
}

.invited-text, .added-text {
  font-size: 10px;
  font-weight: 500;
}

/* 团队信息显示样式 */
.team-info-section, .team-members-section, .pending-invitations-section {
  margin-bottom: 20px;
  padding: 16px;
  background: #f8f9fa;
  border-radius: 8px;
  border: 1px solid #e9ecef;
}

.section-title {
  font-size: 16px;
  font-weight: 600;
  color: #333;
  margin-bottom: 12px;
  display: block;
}

.team-info-item {
  margin-bottom: 12px;
  display: flex;
  align-items: center;
  gap: 8px;
}

.info-label {
  font-size: 14px;
  font-weight: 500;
  color: #666;
  min-width: 80px;
}

.info-value {
  font-size: 14px;
  color: #333;
}

.team-name-edit {
  display: flex;
  align-items: center;
  gap: 8px;
  flex: 1;
}

.team-name-input {
  flex: 1;
  padding: 8px 12px;
  border: 1px solid #ddd;
  border-radius: 4px;
  font-size: 14px;
}

.save-name-btn {
  padding: 8px 16px;
  background: #4CAF50;
  color: white;
  border: none;
  border-radius: 4px;
  font-size: 12px;
  cursor: pointer;
  transition: background 0.3s ease;
}

.save-name-btn:hover:not(:disabled) {
  background: #45a049;
}

.save-name-btn:disabled {
  background: #ccc;
  cursor: not-allowed;
}

/* 成员列表样式 */
.members-list, .invitations-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.member-item, .invitation-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 8px;
  background: white;
  border-radius: 6px;
  border: 1px solid #e9ecef;
}

.member-avatar, .invitation-avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  object-fit: cover;
}

.member-info, .invitation-info {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.member-name, .invitation-name {
  font-size: 14px;
  font-weight: 500;
  color: #333;
}

.member-role {
  font-size: 12px;
  color: #666;
}

.member-status {
  font-size: 12px;
  padding: 2px 6px;
  border-radius: 3px;
  font-weight: 500;
}

.status-0 {
  background: #fff3cd;
  color: #856404;
}

.status-1 {
  background: #d4edda;
  color: #155724;
}

.status-2 {
  background: #f8d7da;
  color: #721c24;
}

.status-3 {
  background: #d1ecf1;
  color: #0c5460;
}

.invitation-time {
  font-size: 12px;
  color: #999;
}

/* 已选择好友显示样式 */
.selected-friends-display {
  margin-bottom: 16px;
  padding: 12px;
  background: #f8f9fa;
  border-radius: 8px;
  border: 1px solid #e9ecef;
}

.selected-title {
  font-size: 14px;
  font-weight: 600;
  color: #333;
  margin-bottom: 8px;
  display: block;
}

.selected-friends-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.selected-friend-item {
  display: flex;
  align-items: center;
  background: white;
  padding: 8px 12px;
  border-radius: 6px;
  border: 1px solid #dee2e6;
}

.selected-friend-avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  margin-right: 12px;
  object-fit: cover;
}

.selected-friend-name {
  flex: 1;
  font-size: 14px;
  color: #333;
}

.remove-selected-btn {
  width: 20px;
  height: 20px;
  border: none;
  background: #ff5722;
  color: white;
  border-radius: 50%;
  font-size: 12px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}

</style>
